/**
 *                            License
 * THE WORK (AS DEFINED BELOW) IS PROVIDED UNDER THE TERMS OF THIS  
 * CREATIVE COMMONS PUBLIC LICENSE ("CCPL" OR "LICENSE"). 
 * THE WORK IS PROTECTED BY COPYRIGHT AND/OR OTHER APPLICABLE LAW.  
 * ANY USE OF THE WORK OTHER THAN AS AUTHORIZED UNDER THIS LICENSE OR  
 * COPYRIGHT LAW IS PROHIBITED.
 * 
 * BY EXERCISING ANY RIGHTS TO THE WORK PROVIDED HERE, YOU ACCEPT AND  
 * AGREE TO BE BOUND BY THE TERMS OF THIS LICENSE. TO THE EXTENT THIS LICENSE  
 * MAY BE CONSIDERED TO BE A CONTRACT, THE LICENSOR GRANTS YOU THE RIGHTS CONTAINED 
 * HERE IN CONSIDERATION OF YOUR ACCEPTANCE OF SUCH TERMS AND CONDITIONS.
 * 
 */
package com.lineage.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.lineage.L1DatabaseFactory;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.utils.SQLUtil;

/**
 * 旅馆钥匙资料表
 */
public class InnKeyTable {

    private static Logger _log = Logger.getLogger(InnKeyTable.class.getName());

    /**
     * 检查钥匙
     * 
     * @param item
     * @return
     */
    public static boolean checkey(final L1ItemInstance item) {
        Connection con = null;
        PreparedStatement pstm = null;
        ResultSet rs = null;
        try {
            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = con
                    .prepareStatement("SELECT * FROM inn_key WHERE item_obj_id=?");

            pstm.setInt(1, item.getId());
            rs = pstm.executeQuery();
            while (rs.next()) {
                final int itemObj = rs.getInt("item_obj_id");
                if (item.getId() == itemObj) {
                    item.setKeyId(rs.getInt("key_id"));
                    item.setInnNpcId(rs.getInt("npc_id"));
                    item.setHall(rs.getBoolean("hall"));
                    item.setDueTime(rs.getTimestamp("due_time"));
                    return true;
                }
            }
        } catch (final SQLException e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            SQLUtil.close(rs);
            SQLUtil.close(pstm);
            SQLUtil.close(con);
        }
        return false;
    }

    /**
     * 删除钥匙
     * 
     * @param item
     */
    public static void deleteKey(final L1ItemInstance item) {
        java.sql.Connection con = null;
        PreparedStatement pstm = null;
        try {
            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = con
                    .prepareStatement("DELETE FROM inn_key WHERE item_obj_id=?");
            pstm.setInt(1, item.getId());
            pstm.execute();
        } catch (final SQLException e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            SQLUtil.close(pstm);
            SQLUtil.close(con);

        }
    }

    /**
     * 储存钥匙
     * 
     * @param item
     */
    public static void storeKey(final L1ItemInstance item) {
        java.sql.Connection con = null;
        PreparedStatement pstm = null;
        try {
            con = L1DatabaseFactory.getInstance().getConnection();
            pstm = con
                    .prepareStatement("INSERT INTO inn_key SET item_obj_id=?, key_id=?, npc_id=?, hall=?, due_time=?");

            pstm.setInt(1, item.getId());
            pstm.setInt(2, item.getKeyId());
            pstm.setInt(3, item.getInnNpcId());
            pstm.setBoolean(4, item.checkRoomOrHall());
            pstm.setTimestamp(5, item.getDueTime());
            pstm.execute();
        } catch (final SQLException e) {
            _log.log(Level.SEVERE, e.getLocalizedMessage(), e);
        } finally {
            SQLUtil.close(pstm);
            SQLUtil.close(con);
        }
    }

    private InnKeyTable() {
    }

}
